package com.wu.crud.service;

import com.wu.crud.bean.Student;
import com.wu.crud.bean.StudentExample;
import com.wu.crud.bean.StudentExample.Criteria;
import com.wu.crud.dao.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    StudentMapper studentMapper;

    /**
     * 查询所有学生
     */
    public List<Student> getAllStu(){
        return studentMapper.selectByExample(null);
    }

    /**
     * 添加学生
     * @param student
     */
    public void addStu(Student student){
        studentMapper.insertSelective(student);
    }

    /**
     * 检验名字是否可用
     * @param stuName
     * @return
     */
    public boolean checkAdmin(String stuName) {
        // TODO Auto-generated method stub
        StudentExample example = new StudentExample();
        Criteria criteria = example.createCriteria();
        criteria.andSnameEqualTo(stuName);
        long count = studentMapper.countByExample(example);
        return count == 0;
    }

    public Student getStu(String sid){
        return studentMapper.selectByPrimaryKey(sid);
    }
}
